
<html>
      <head><title>CS143 Project 1B Demo</title></head>
<body>

<h1> Web Query by Yiqi Yu</h1>
Type an SQL query in the following box:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="Submit" />
</form>

<p><small>Note: tables and fields are case sensitive. Run "show tables" to see the list of available tables.</small>
</p>

<?php

if($_GET["query"])
{
	$db_connect = mysql_connect("localhost", "cs143", "");
	mysql_select_db("CS143", $db_connect);
	$QueryIn = $_GET["query"];
	$result = mysql_query("$QueryIn", $db_connect);
	
	$numOfFields = @mysql_num_fields($result);
	
	echo "<h3> Results from MySQL: </h3>";
	echo "<table border=1 cellspacing=1 cellpadding=2>";
	
	echo "<tr align=center>";
	for($i = 0; $i < $numOfFields; $i++)
	{
		$field = mysql_fetch_field($result);
		echo "<th><b>$field->name</b></th>";
	}
	echo "</tr>\n";
	
	while($row = @mysql_fetch_row($result))
	{
		echo "<tr align=center>";
		foreach($row as $cell) echo "<td>$cell</td>";
		echo "</tr>\n";
	}
	echo "</table>";
	mysql_close($db_connect);
}

?>

</body>
</html>

